home *** CD-ROM | disk | FTP | other *** search
/ Mac Easy 2010 May / Mac Life Ubuntu.iso / casper / filesystem.squashfs / usr / lib / python2.6 / distutils / archive_util.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2009-04-20  |  5.3 KB  |  165 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.6)
  3.  
  4. '''distutils.archive_util
  5.  
  6. Utility functions for creating archive files (tarballs, zip files,
  7. that sort of thing).'''
  8. __revision__ = '$Id: archive_util.py 62904 2008-05-08 22:09:54Z benjamin.peterson $'
  9. import os
  10. from distutils.errors import DistutilsExecError
  11. from distutils.spawn import spawn
  12. from distutils.dir_util import mkpath
  13. from distutils import log
  14.  
  15. def make_tarball(base_name, base_dir, compress = 'gzip', verbose = 0, dry_run = 0):
  16.     '''Create a (possibly compressed) tar file from all the files under
  17.     \'base_dir\'.  \'compress\' must be "gzip" (the default), "compress",
  18.     "bzip2", or None.  Both "tar" and the compression utility named by
  19.     \'compress\' must be on the default program search path, so this is
  20.     probably Unix-specific.  The output tar file will be named \'base_dir\' +
  21.     ".tar", possibly plus the appropriate compression extension (".gz",
  22.     ".bz2" or ".Z").  Return the output filename.
  23.     '''
  24.     compress_ext = {
  25.         'gzip': '.gz',
  26.         'bzip2': '.bz2',
  27.         'compress': '.Z' }
  28.     compress_flags = {
  29.         'gzip': [
  30.             '-f9'],
  31.         'compress': [
  32.             '-f'],
  33.         'bzip2': [
  34.             '-f9'] }
  35.     if compress is not None and compress not in compress_ext.keys():
  36.         raise ValueError, "bad value for 'compress': must be None, 'gzip', or 'compress'"
  37.     compress not in compress_ext.keys()
  38.     archive_name = base_name + '.tar'
  39.     mkpath(os.path.dirname(archive_name), dry_run = dry_run)
  40.     cmd = [
  41.         'tar',
  42.         '-cf',
  43.         archive_name,
  44.         base_dir]
  45.     spawn(cmd, dry_run = dry_run)
  46.     if compress:
  47.         spawn([
  48.             compress] + compress_flags[compress] + [
  49.             archive_name], dry_run = dry_run)
  50.         return archive_name + compress_ext[compress]
  51.     return archive_name
  52.  
  53.  
  54. def make_zipfile(base_name, base_dir, verbose = 0, dry_run = 0):
  55.     '''Create a zip file from all the files under \'base_dir\'.  The output
  56.     zip file will be named \'base_dir\' + ".zip".  Uses either the "zipfile"
  57.     Python module (if available) or the InfoZIP "zip" utility (if installed
  58.     and found on the default search path).  If neither tool is available,
  59.     raises DistutilsExecError.  Returns the name of the output zip file.
  60.     '''
  61.     
  62.     try:
  63.         import zipfile
  64.     except ImportError:
  65.         zipfile = None
  66.  
  67.     zip_filename = base_name + '.zip'
  68.     mkpath(os.path.dirname(zip_filename), dry_run = dry_run)
  69.     if zipfile is None:
  70.         if verbose:
  71.             zipoptions = '-r'
  72.         else:
  73.             zipoptions = '-rq'
  74.         
  75.         try:
  76.             spawn([
  77.                 'zip',
  78.                 zipoptions,
  79.                 zip_filename,
  80.                 base_dir], dry_run = dry_run)
  81.         except DistutilsExecError:
  82.             raise DistutilsExecError, "unable to create zip file '%s': could neither import the 'zipfile' module nor find a standalone zip utility" % zip_filename
  83.         except:
  84.             None<EXCEPTION MATCH>DistutilsExecError
  85.         
  86.  
  87.     None<EXCEPTION MATCH>DistutilsExecError
  88.     log.info("creating '%s' and adding '%s' to it", zip_filename, base_dir)
  89.     if not dry_run:
  90.         z = zipfile.ZipFile(zip_filename, 'w', compression = zipfile.ZIP_DEFLATED)
  91.         for dirpath, dirnames, filenames in os.walk(base_dir):
  92.             for name in filenames:
  93.                 path = os.path.normpath(os.path.join(dirpath, name))
  94.                 if os.path.isfile(path):
  95.                     z.write(path, path)
  96.                     log.info("adding '%s'" % path)
  97.                     continue
  98.             
  99.         
  100.         z.close()
  101.     
  102.     return zip_filename
  103.  
  104. ARCHIVE_FORMATS = {
  105.     'gztar': (make_tarball, [
  106.         ('compress', 'gzip')], "gzip'ed tar-file"),
  107.     'bztar': (make_tarball, [
  108.         ('compress', 'bzip2')], "bzip2'ed tar-file"),
  109.     'ztar': (make_tarball, [
  110.         ('compress', 'compress')], 'compressed tar file'),
  111.     'tar': (make_tarball, [
  112.         ('compress', None)], 'uncompressed tar file'),
  113.     'zip': (make_zipfile, [], 'ZIP file') }
  114.  
  115. def check_archive_formats(formats):
  116.     for format in formats:
  117.         if format not in ARCHIVE_FORMATS:
  118.             return format
  119.     else:
  120.         return None
  121.     return format not in ARCHIVE_FORMATS
  122.  
  123.  
  124. def make_archive(base_name, format, root_dir = None, base_dir = None, verbose = 0, dry_run = 0):
  125.     '''Create an archive file (eg. zip or tar).  \'base_name\' is the name
  126.     of the file to create, minus any format-specific extension; \'format\'
  127.     is the archive format: one of "zip", "tar", "ztar", or "gztar".
  128.     \'root_dir\' is a directory that will be the root directory of the
  129.     archive; ie. we typically chdir into \'root_dir\' before creating the
  130.     archive.  \'base_dir\' is the directory where we start archiving from;
  131.     ie. \'base_dir\' will be the common prefix of all files and
  132.     directories in the archive.  \'root_dir\' and \'base_dir\' both default
  133.     to the current directory.  Returns the name of the archive file.
  134.     '''
  135.     save_cwd = os.getcwd()
  136.     if root_dir is not None:
  137.         log.debug("changing into '%s'", root_dir)
  138.         base_name = os.path.abspath(base_name)
  139.         if not dry_run:
  140.             os.chdir(root_dir)
  141.         
  142.     
  143.     if base_dir is None:
  144.         base_dir = os.curdir
  145.     
  146.     kwargs = {
  147.         'dry_run': dry_run }
  148.     
  149.     try:
  150.         format_info = ARCHIVE_FORMATS[format]
  151.     except KeyError:
  152.         raise ValueError, "unknown archive format '%s'" % format
  153.  
  154.     func = format_info[0]
  155.     for arg, val in format_info[1]:
  156.         kwargs[arg] = val
  157.     
  158.     filename = apply(func, (base_name, base_dir), kwargs)
  159.     if root_dir is not None:
  160.         log.debug("changing back to '%s'", save_cwd)
  161.         os.chdir(save_cwd)
  162.     
  163.     return filename
  164.  
  165.